package com.yaojiaxun.logisticsPlatformSystem.controller;

import com.yaojiaxun.logisticsPlatformSystem.entity.ClientInfo;
import com.yaojiaxun.logisticsPlatformSystem.service.ClientInfoService;
import com.yaojiaxun.logisticsPlatformSystem.service.OrderInfoService;
import com.yaojiaxun.logisticsPlatformSystem.util.ResultVOUtil;
import com.yaojiaxun.logisticsPlatformSystem.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @Auther: yjx_j
 * @Date: 2019/2/12 15:07
 * @Description:
 */
@Controller
public class ClientInfoController {
    @Autowired
    private ClientInfoService clientInfoService;

    @Autowired
    private OrderInfoService orderInfoService;

    //客户信息管理
    @RequestMapping("/client_manage")
    public String clientManageIndex(@RequestParam(defaultValue = "1") Integer page, Map<String ,Object> map){
        map.put("pageDTO", clientInfoService.findPage(page));
        return "system/client/client_manage";
    }

    @RequestMapping("/clientAddBefore")
    public String clientAddBefore(){
        return "system/client/client_add";
    }

    @RequestMapping("/client_add_after")
    public String clientAddAfter(ClientInfo clientInfo){
        if (clientInfoService.findClientByClientAccount(clientInfo.getClientAccount()) != null){
            return "system/client/client_add_error";
        }
        clientInfoService.insertClientInfo(clientInfo);
        return "redirect:/client_manage";
    }

    @RequestMapping("/client_detail")
    public String getDetails(@RequestParam("clientId") Long clientId, @RequestParam("page") Integer page, Map<String, Object> map){
        map.put("page", page);
        map.put("clientInfo", clientInfoService.findClientById(clientId));
        return "system/client/client_detail";
    }

    @RequestMapping("/client_update_after")
    public String updateClientAfter(@RequestParam("page") Integer page, ClientInfo clientInfo){
        clientInfoService.updateClientInfo(clientInfo);
        return "redirect:/client_manage?page="+page;
    }

    @RequestMapping("/clientDelete")
    public String deleteClient(@RequestParam("clientId") Long clientId, @RequestParam("page") Integer page){
        clientInfoService.deleteClientById(clientId);
        return "redirect:/client_manage?page="+page;
    }

    @RequestMapping("/findByClientAccount")
    public String findByClientAccount(@RequestParam("clientAccount") String clientAccount, Map<String, Object> map){
        map.put("tiaojian", clientAccount);
        map.put("results", clientInfoService.clientSearch(clientAccount));
        return "system/client/client_search_results";
    }

    //客户注册页面
    @RequestMapping("/clientRegister")
    public String logClientRegister(HttpSession session){
        session.setAttribute("user", null);
        return "front/log/log_client_register";
    }
    //用户注册完成
    @RequestMapping("/clientRegisterAfter")
    public String logClientRegisterAfter(ClientInfo clientInfo){
        if (clientInfoService.findClientByClientAccount(clientInfo.getClientAccount()) != null){
            return "system/client/client_add_error";
        }
        clientInfoService.insertClientInfo(clientInfo);
        return "redirect:/logisticsPlatform/logIn";
    }
    @ResponseBody
    @RequestMapping("/checkAccount")
    public ResultVO checkAccount(@RequestParam("account") String account){
        return ResultVOUtil.success(clientInfoService.findClientByClientAccount(account));
    }

    //跳转到客户中心页面(默认打开个人资料栏)
    @RequestMapping("/clientCenter")
    public String goClientCenter(@RequestParam("clientAccount") String clientAccount, Map<String, Object> map, HttpSession session){
        if (session.getAttribute("user") == null){
            return "redirect:/logisticsPlatform/logIn";
        }
        map.put("clientInfo", clientInfoService.findClientByClientAccountTimeFormat(clientAccount));
        return "front/client/client_center";
    }

    //客户自行修改个人信息
    @RequestMapping("/client_xiugai")
    public String clientXiuGai(@RequestParam("clientAccount") String clientAccount, ClientInfo clientInfo, HttpSession session){
        if (session.getAttribute("user") == null){
            return "redirect:/logisticsPlatform/logIn";
        }
        clientInfoService.updateClientInfo(clientInfo);
        return "redirect:/clientCenter?clientAccount="+clientAccount;
    }

    //跳转到客户所有订单页面
    @RequestMapping("/clientCenterOrderAll")
    public String goClientOrderAll(@RequestParam("clientAccount") String clientAccount, Map<String, Object> map, HttpSession session){
        if (session.getAttribute("user") == null){
            return "redirect:/logisticsPlatform/logIn";
        }
        map.put("clientInfo", clientInfoService.findClientByClientAccountTimeFormat(clientAccount));
        map.put("finishedOrderInfo", orderInfoService.findFinishedOrdersByClientAccount(clientAccount));//已完成订单
        map.put("unfinishedOrderInfo", orderInfoService.findUnfinishedOrdersByClientAccount(clientAccount));//未接单订单
        map.put("receivedOrderInfo", orderInfoService.findReceivedOrdersByClientAccount(clientAccount));//已接单订单
        map.put("refusedOrderInfo", orderInfoService.findRefusedOrdersByClientAccount(clientAccount));//问题订单(被拒绝订单)
        return "front/client/client_order_all";
    }

    //跳转到客户已完成订单页面
    @RequestMapping("/clientCenterOrderFinished")
    public String goClientOrderFinished(@RequestParam("clientAccount") String clientAccount, Map<String, Object> map, HttpSession session){
        if (session.getAttribute("user") == null){
            return "redirect:/logisticsPlatform/logIn";
        }
        map.put("clientInfo", clientInfoService.findClientByClientAccountTimeFormat(clientAccount));
        map.put("finishedOrderInfo", orderInfoService.findFinishedOrdersByClientAccount(clientAccount));//已完成订单
        return "front/client/client_order_finished";
    }

    //跳转到客户未接单订单页面
    @RequestMapping("/clientCenterOrderUnfinished")
    public String goClientOrderUnfinished(@RequestParam("clientAccount") String clientAccount, Map<String, Object> map, HttpSession session){
        if (session.getAttribute("user") == null){
            return "redirect:/logisticsPlatform/logIn";
        }
        map.put("clientInfo", clientInfoService.findClientByClientAccountTimeFormat(clientAccount));
        map.put("unfinishedOrderInfo", orderInfoService.findUnfinishedOrdersByClientAccount(clientAccount));//未接单订单
        return "front/client/client_order_unfinished";
    }

    //跳转到客户问题订单(被拒绝订单)页面
    @RequestMapping("/clientCenterOrderRefused")
    public String goClientOrderRefused(@RequestParam("clientAccount") String clientAccount, Map<String, Object> map, HttpSession session){
        if (session.getAttribute("user") == null){
            return "redirect:/logisticsPlatform/logIn";
        }
        map.put("clientInfo", clientInfoService.findClientByClientAccountTimeFormat(clientAccount));
        map.put("refusedOrderInfo", orderInfoService.findRefusedOrdersByClientAccount(clientAccount));//被拒绝订单
        return "front/client/client_order_refused";
    }

    //跳转到客户已接单页面
    @RequestMapping("/clientCenterOrderReceived")
    public String goClientOrderReceived(@RequestParam("clientAccount") String clientAccount, Map<String, Object> map, HttpSession session){
        if (session.getAttribute("user") == null){
            return "redirect:/logisticsPlatform/logIn";
        }
        map.put("clientInfo", clientInfoService.findClientByClientAccountTimeFormat(clientAccount));
        map.put("receivedOrderInfo", orderInfoService.findReceivedOrdersByClientAccount(clientAccount));//已接单订单
        return "front/client/client_order_received";
    }
}
